package org.example.project.search

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.tencent.compose.map.poi.ui.SearchRecordCard
import org.example.project.search.data.AppButton
import org.example.project.search.data.LatLng
import org.example.project.search.data.MiniApp
import org.example.project.search.data.SgPassLine
import org.example.project.search.data.Suggestion
import org.example.project.search.ui.HistoryRecordCard
import org.example.project.search.ui.HotSearchList
import org.example.project.search.ui.SearchCard
import org.example.project.search.ui.SearchFoundCard
import org.example.project.search.ui.SpecialTopicCard

@Composable
fun SearchDemo(modifier: Modifier = Modifier) {


    var text by remember { mutableStateOf("") }
    // 类型
    val types = listOf("美食", "酒店", "景点", "加油站", "超市", "更多")
    // 历史记录
    val historyRecords = listOf(
        Suggestion(
            name = "北京市",
            highlightText = "北京",
            address = "北京市",
            distance = "31公里",
            type = 12,
            isOfflineData = true
        ),
        Suggestion(
            name = "天安门广场预约参观",
            type = 18,
            isOfflineData = true,
        ),
        Suggestion(
            name = "天安门东(公交站)",
            type = 23,
            isOfflineData = true
        ),
        Suggestion(
            name = "大钟寺(地铁站)",
            type = 21,
            isOfflineData = true
        ),
        Suggestion(
            name = "499路", showName = "燕莎桥南--草房西路南口", type = 24,
            isOfflineData = true
        ),Suggestion(
            name = "北京市",
            highlightText = "北京",
            address = "北京市",
            distance = "31公里",
            type = 12,
            isOfflineData = true
        ),
        Suggestion(
            name = "天安门广场预约参观",
            type = 18,
            isOfflineData = true,
        ),
        Suggestion(
            name = "天安门东(公交站)",
            type = 23,
            isOfflineData = true
        ),
        Suggestion(
            name = "大钟寺(地铁站)",
            type = 21,
            isOfflineData = true
        ),
        Suggestion(
            name = "499路", showName = "燕莎桥南--草房西路南口", type = 24,
            isOfflineData = true
        ),Suggestion(
            name = "北京市",
            highlightText = "北京",
            address = "北京市",
            distance = "31公里",
            type = 12,
            isOfflineData = true
        ),
        Suggestion(
            name = "天安门广场预约参观",
            type = 18,
            isOfflineData = true,
        ),
        Suggestion(
            name = "天安门东(公交站)",
            type = 23,
            isOfflineData = true
        ),
        Suggestion(
            name = "大钟寺(地铁站)",
            type = 21,
            isOfflineData = true
        ),
        Suggestion(
            name = "499路", showName = "燕莎桥南--草房西路南口", type = 24,
            isOfflineData = true
        ),Suggestion(
            name = "北京市",
            highlightText = "北京",
            address = "北京市",
            distance = "31公里",
            type = 12,
            isOfflineData = true
        ),
        Suggestion(
            name = "天安门广场预约参观",
            type = 18,
            isOfflineData = true,
        ),
        Suggestion(
            name = "天安门东(公交站)",
            type = 23,
            isOfflineData = true
        ),
        Suggestion(
            name = "大钟寺(地铁站)",
            type = 21,
            isOfflineData = true
        ),
        Suggestion(
            name = "499路", showName = "燕莎桥南--草房西路南口", type = 24,
            isOfflineData = true
        ),Suggestion(
            name = "北京市",
            highlightText = "北京",
            address = "北京市",
            distance = "31公里",
            type = 12,
            isOfflineData = true
        ),
        Suggestion(
            name = "天安门广场预约参观",
            type = 18,
            isOfflineData = true,
        ),
        Suggestion(
            name = "天安门东(公交站)",
            type = 23,
            isOfflineData = true
        ),
        Suggestion(
            name = "大钟寺(地铁站)",
            type = 21,
            isOfflineData = true
        ),
        Suggestion(
            name = "499路", showName = "燕莎桥南--草房西路南口", type = 24,
            isOfflineData = true
        ),
    )
    val historyList = remember { mutableStateOf(historyRecords) }
    // 搜索页面
    val founds = listOf(
        "北京",
        "腾讯大厦",
        "麦当劳",
        "奥林匹克公园",
        "博纳国际影城",
        "麦当劳",
        "奥林匹克公园"
    )
    // 普通搜索记录
    val sugDatas = listOf(
        Suggestion(
            name = "猜你喜欢",
            type = 20,
        ),
        Suggestion(
            name = "海底捞火锅海底捞火12312. 锅海底捞火锅海底捞火锅海底捞火锅海底捞火锅海底捞 c.  火锅海底捞火锅海底捞火锅",
            type = 17,
            highlightText = "海底捞"
        ),
        Suggestion(
            name = "南山路附近",
            address = "北京市门头沟区南山路",
            distance = "330000",
            type = 29,
            latLng = LatLng(40.0, 116.0)
        ),
        Suggestion(
            name = "北京市",
            highlightText = "北京",
            address = "北京市",
            distance = "310000",
            type = 12,
        ),
        Suggestion(
            name = "天坛公园",
            highlightText = "天园",
            address = "东城区-天坛东路甲1路",
            type = 11,
            distance = "141000",
            starLevel = 98,
            typeWord = "5A级景点",
            peopleFlow = "实时人流量小",
            behavior = "1分钟前有人想去",
            priceInfo = "¥148/人",
            poiErrInfo = "5A景区",
            picUrl = "xx",
            miniApp = MiniApp(),
            subSuggestions = listOf(
                Suggestion(
                    name = "天坛公园-东门",
                    showName = "东门",
                ),
                Suggestion(
                    name = "天坛公园-南门",
                    showName = "南门",
                ),
                Suggestion(
                    name = "天坛公园-北门",
                    showName = "北门",
                ),
                Suggestion(
                    name = "天坛公园-西门",
                    showName = "西门",
                ),
            )
        ),
        Suggestion(
            name = "天坛公园-祈年殿天坛公园-祈年殿天坛公园-祈年殿天坛公园-祈年殿天坛公园-祈年殿天坛公园-祈年殿天坛公园-祈年殿",
            highlightText = "殿",
            address = "东城区",
            type = 11,
            distance = "141000",
            starLevel = 98,
            typeWord = "文物古迹",
            peopleFlow = "实时人流量小",
            behavior = "1分钟前有人想去",
            poiErrInfo = "5A景区",
            priceInfo = "¥148/人",
        ),
        Suggestion(
            name = "天安门广场预约参观",
            type = 18,
            miniApp = MiniApp(
                appButton = listOf(
                    AppButton(button_name = "在线预约"),
                )
            )
        ),
//        Suggestion(
//            type = 10,
//            footerText = "查看全部信息"
//        )
    )
    // 合并搜索记录
    val mergeSugDatas = listOf(
        Suggestion(
            cityName = "北京市",
            shortCityName = "北京",
            suggestionsInCity = listOf(
                Suggestion(
                    name = "天坛公园",
                    highlightText = "天坛",
                    address = "东城区-天坛东路甲1路",
                    type = 11,
                    distance = "1209",
                    starLevel = 98,
                    typeWord = "5A级景点",
                    peopleFlow = "实时人流量小",
                    behavior = "1分钟前有人想去",
                    poiErrInfo = "5A景区",
                    picUrl = "https://multimedia.map.qq.com/mapbigdata/0/142127a0a9c138184676b4afea78ace4/500",
                    miniApp = MiniApp(),
                    subSuggestions = listOf(
                        Suggestion(
                            name = "天坛公园-东门",
                            showName = "东门",
                        ),
                        Suggestion(
                            name = "天坛公园-南门",
                            showName = "南门",
                        ),
                        Suggestion(
                            name = "天坛公园-北门",
                            showName = "北门",
                        ),
                        Suggestion(
                            name = "天坛公园-西门",
                            showName = "西门",
                        ),
                    )
                ),
                Suggestion(
                    name = "天坛公园-祈年殿",
                    highlightText = "殿",
                    address = "东城区",
                    type = 11,
                    distance = "9430",
                    starLevel = 98,
                    typeWord = "文物古迹",
                    peopleFlow = "实时人流量小",
                    behavior = "1分钟前有人想去",
                    poiErrInfo = "5A景区",
                ),
                Suggestion(
                    name = "天安门广场预约参观",
                    type = 18,
                ),
                Suggestion(
                    name = "腾讯北京总部大楼",
                    highlightText = "北京",
                    address = "北京市海淀区西北旺东路10号院西区9号院123123",
                    type = 11,
                    typeWord = "写字楼",
                    distance = "120",
                    subSuggestions = listOf(
                        Suggestion(
                            name = "腾讯北京总部大楼-东南门",
                            showName = "东南门",
                        ),
                        Suggestion(
                            name = "腾讯北京总部大楼-楼宇东北门",
                            showName = "楼宇东北门",
                        ),
                        Suggestion(
                            name = "腾讯北京总部大楼-西南2门",
                            showName = "西南2门",
                        ),
                        Suggestion(
                            name = "腾讯北京总部大楼-地下停车场",
                            showName = "地下停车场",
                        ),
                    )
                ),
                Suggestion(
                    name = "天安门东(公交站)",
                    type = 23,
                    distance = "13809",
                    sgPassLines = listOf(
                        SgPassLine(strName = "499路", strColor = "#4875fd"),
                        SgPassLine(strName = "675路", strColor = "#4875fd"),
                        SgPassLine(strName = "911路", strColor = "#4875fd"),
                        SgPassLine(strName = "517路", strColor = "#4875fd"),
                        SgPassLine(strName = "220路", strColor = "#4875fd"),
                        SgPassLine(strName = "220路", strColor = "#4875fd"),
                    )

                ),
                Suggestion(name = "Coco都可", address = "北京市朝阳区国奥中心xxxxxx", type = 16),
                Suggestion(name = "天安门", address = "北京市朝阳区国奥中心xxxxxx"),
                Suggestion(
                    name = "黄渠村(公交站)",
                    type = 23,
                    distance = "897"
                ),
                Suggestion(
                    name = "南锣鼓巷(地铁站)",
                    type = 21,
                    distance = "987",
                    sgPassLines = listOf(
                        SgPassLine(strName = "6号线", strColor = "#ab8735"),
                        SgPassLine(strName = "8号线", strColor = "#43956b")
                    )
                ),
                Suggestion(
                    name = "地铁13号线",
                    showName = "东直门--西直门",
                    type = 22,
                    distance = "894"
                ),
                Suggestion(
                    name = "499路",
                    showName = "燕莎桥南--草房西路南口",
                    type = 24,
                    distance = "987"
                ),
            )
        ),
        Suggestion(
            cityName = "深圳市",
            shortCityName = "深圳",
            suggestionsInCity = listOf(
                Suggestion(
                    name = "腾讯大厦",
                    address = "南山区-深海大道10000号",
                    type = 11,
                    typeWord = "写字楼",
                    distance = "9742000"
                )
            )
        ),
        Suggestion(
            type = 10,
            footerText = "查看全部信息"
        )
    )

    var list: MutableList<Suggestion> = mutableListOf()
    val rSugList = remember { sugDatas }
    // 榜单列表
    val hotSearchList by remember {
        mutableStateOf(
            listOf(
                Pair("宝屋日料", "人气10.1万"),
                Pair("宝屋日料", "人气10.1万"),
                Pair("宝屋日料", "人气10.1万"),
                Pair("宝屋日料", "人气10.1万"),
                Pair("宝屋日料", "人气10.1万"),
                Pair("宝屋日料", "人气10.1万"),
                Pair("宝屋日料", "人气10.1万"),
                Pair("宝屋日料", "人气10.1万"),
                Pair("宝屋日料", "人气10.1万"),
                Pair("宝屋日料", "人气10.1万"),
            )
        )
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(color = Color(0xfff1f3f5))
            .padding(start = 12.dp, top = 8.dp, end = 12.dp, bottom = 8.dp),
    ) {

        val listState = rememberLazyListState()

        // 是否有搜索文本
        val hasText by remember {
            derivedStateOf { text != "" }
        }
        // 是否在顶部
        val isTop by remember {
            derivedStateOf { listState.firstVisibleItemIndex == 0 }
        }

        // 头部搜索框
        SearchCard(
            text,
            isTop,
            hasText,
            {
            },
            { text = it },
            { text = "" }
        )
        Spacer(Modifier.height(1.dp))

        // 搜索后
        if (hasText && rSugList.isNotEmpty()) {
            SearchRecordCard(rSugList)
        }

        // 搜索前
        if (!hasText) {
            LazyColumn(
                modifier = Modifier.fillMaxSize().padding(bottom = 10.dp),
                state = listState
            ) {
                item {
                    SpecialTopicCard(types)
                }
                item {
                    HistoryRecordCard(historyList)
                    SearchFoundCard(founds, { title -> text = title })
                }
                item {
                    HotSearchList("周边美食热搜榜", hotSearchList)
                }
                item {
                    Spacer(Modifier.height(20.dp))
                }
            }
        }
    }
}